package cz.muni.fi.pa165.library.client.gui.swing;

import cz.muni.fi.pa165.library.api.ReaderService;
import cz.muni.fi.pa165.library.api.ReaderTO;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author xfoltyn1
 */
public class ReaderPanel extends JPanel {

    private ReaderPanel readerPanel;
    private ReaderService readerWebService;
    private JButton createReaderButton, addReaderButton, removeBookButton, updateReaderButton;
    private DefaultTableModel model;
    private JPanel createPanel;
    private JFrame frameCreate;
    private JTable table;
    private ReaderTO reader;
    private JTextField firstNameField, lastNameField, birthNumberField, addressField, emailField, phoneField;
    private JLabel firstNameLabel, lastNameLabel, birthNumberLabel, addressLabel, emailLabel, phoneLabel;

    public ReaderPanel(ReaderService readerWebService) {        
        this.readerWebService = readerWebService;
        readerPanel = this;
        
        initializeComponents();

        setLayout(null);
        addReaderButton.setBounds(50, 20, 150, 20);
        add(addReaderButton);
        removeBookButton.setBounds(220, 20, 150, 20);
        add(removeBookButton);
        updateReaderButton.setBounds(390, 20, 150, 20);
        add(updateReaderButton);
        JScrollPane pane = new JScrollPane(table);
        pane.setBounds(50, 50, 670, 300);
        add(pane);
    }

    private void parseReader(List<ReaderTO> list) {
        Iterator it = list.listIterator();
        while (it.hasNext()) {
            ReaderTO readerTO = (ReaderTO) it.next();
            Object[] readerRow = {
                readerTO.getId(), readerTO.getFirstName(), readerTO.getSurname(),
                readerTO.getBirthNumber(), readerTO.getAddress(), readerTO.getEmail(), readerTO.getTelephoneNumber()
            };
            model.addRow(readerRow);
        }
    }

    private void readerTable() {
        model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        model.addColumn("ID");
        model.addColumn("Name");
        model.addColumn("Surname");
        model.addColumn("Birth number");
        model.addColumn("Address");
        model.addColumn("E-mail");
        model.addColumn("Phone number");
        try {
            List<ReaderTO> list = readerWebService.findAllReaders();
            if (list != null) {
                parseReader(readerWebService.findAllReaders());
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(readerPanel, "Cannot get readers.\nServer connection problem",
                    "Oops!", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void initializeComponents() {
        readerTable();
        table = new JTable(model);

        addReaderButton = new JButton("Add reader");
        addReaderButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frameCreate = new JFrame("Add reader");
                frameCreate.add(new JScrollPane(createReader()), BorderLayout.CENTER);
                frameCreate.setVisible(true);
                frameCreate.setSize(new Dimension(400, 450));
                frameCreate.setLocation(Utils.getPointToCenterApplication(400, 500));
            }
        });
        addReaderButton.setVisible(true);

        updateReaderButton = new JButton("Update reader");
        updateReaderButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (table.getSelectedRow() != -1) {
                    frameCreate = new JFrame("Update reader");
                    frameCreate.add(new JScrollPane(updateReader()), BorderLayout.CENTER);
                    frameCreate.setVisible(true);
                    frameCreate.setSize(new Dimension(400, 450));
                    frameCreate.setLocation(Utils.getPointToCenterApplication(400, 500));
                }
            }
        });
        updateReaderButton.setVisible(true);

        removeBookButton = new JButton("Remove reader");
        removeBookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (table.getSelectedRow() != -1) {
                    Long id = (Long) model.getValueAt(table.getSelectedRow(), 0);
                    try {
                        readerWebService.deleteReader(readerWebService.findReaderById(id));
                        model.removeRow(table.getSelectedRow());
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(readerPanel, "Cannot remove reader.\nServer connection problem.",
                                "Ooops!", JOptionPane.ERROR_MESSAGE);
                    }

                }
            }
        });
        removeBookButton.setVisible(true);
    }

    private JPanel updateReader() {
        createPanel = new JPanel();
        Long id = (Long) model.getValueAt(table.getSelectedRow(), 0);
        reader = new ReaderTO();
        try {
            reader = readerWebService.findReaderById(id);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(readerPanel, "Cannot update reader.\nServer connection problem",
                    "Oops!", JOptionPane.ERROR_MESSAGE);
        }

        createPanel.setLayout(null);
        firstNameField = new JTextField(reader.getFirstName());
        lastNameField = new JTextField(reader.getSurname());
        birthNumberField = new JTextField(reader.getBirthNumber());
        addressField = new JTextField(reader.getAddress());
        emailField = new JTextField(reader.getEmail());
        phoneField = new JTextField(reader.getTelephoneNumber());

        firstNameLabel = new JLabel("Name");
        lastNameLabel = new JLabel("Surname");
        birthNumberLabel = new JLabel("Birth number");
        addressLabel = new JLabel("Address");
        emailLabel = new JLabel("E-mail");
        phoneLabel = new JLabel("Phone number");

        firstNameLabel.setBounds(10, 15, 150, 20);
        createPanel.add(firstNameLabel);
        firstNameField.setBounds(10, 35, 150, 20);
        createPanel.add(firstNameField);

        lastNameLabel.setBounds(10, 60, 150, 20);
        createPanel.add(lastNameLabel);
        lastNameField.setBounds(10, 80, 150, 20);
        createPanel.add(lastNameField);

        addressLabel.setBounds(10, 105, 150, 20);
        createPanel.add(addressLabel);
        createPanel.add(addressField);
        addressField.setBounds(10, 125, 150, 20);

        emailLabel.setBounds(10, 150, 150, 20);
        createPanel.add(emailLabel);
        createPanel.add(emailField);
        emailField.setBounds(10, 170, 150, 20);

        phoneLabel.setBounds(10, 195, 150, 20);
        createPanel.add(phoneLabel);
        createPanel.add(phoneField);
        phoneField.setBounds(10, 215, 150, 20);

        birthNumberLabel.setBounds(10, 240, 150, 20);
        createPanel.add(birthNumberLabel);
        birthNumberField.setBounds(10, 260, 150, 20);
        createPanel.add(birthNumberField);

        createReaderButton = new JButton();
        createReaderButton.setText("Update reader");
        createReaderButton.setBounds(10, 310, 150, 20);
        createPanel.add(createReaderButton);
        createReaderButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boolean check = true;
                if (firstNameField.getText().equals("")) {
                    firstNameField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (lastNameField.getText().equals("")) {
                    lastNameField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (birthNumberField.getText().equals("")) {
                    birthNumberField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (addressField.getText().equals("")) {
                    addressField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (emailField.getText().equals("")) {
                    emailField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (phoneField.getText().equals("")) {
                    phoneField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (check) {
                    reader.setFirstName(firstNameField.getText());
                    reader.setSurname(lastNameField.getText());
                    reader.setBirthNumber(birthNumberField.getText());
                    reader.setEmail(emailField.getText());
                    reader.setAddress(addressField.getText());
                    reader.setTelephoneNumber(phoneField.getText());
                    try {
                        readerWebService.updateReader(reader);
                        model.setValueAt(reader.getFirstName(), table.getSelectedRow(), 1);
                        model.setValueAt(reader.getSurname(), table.getSelectedRow(), 2);
                        model.setValueAt(reader.getBirthNumber(), table.getSelectedRow(), 3);
                        model.setValueAt(reader.getAddress(), table.getSelectedRow(), 4);
                        model.setValueAt(reader.getEmail(), table.getSelectedRow(), 5);
                        model.setValueAt(reader.getTelephoneNumber(), table.getSelectedRow(), 6);

                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(readerPanel, "Cannot update reader.\nServer connection problem",
                                "Oops!", JOptionPane.ERROR_MESSAGE);
                    }

                    frameCreate.dispose();
                }
            }
        });
        return createPanel;

    }

    private JPanel createReader() {
        createPanel = new JPanel();
        createPanel.setLayout(null);

        firstNameField = new JTextField();
        lastNameField = new JTextField();
        birthNumberField = new JTextField();
        addressField = new JTextField();
        emailField = new JTextField();
        phoneField = new JTextField();

        firstNameLabel = new JLabel("Name");
        lastNameLabel = new JLabel("Surname");
        birthNumberLabel = new JLabel("Birth number");
        addressLabel = new JLabel("Address");
        emailLabel = new JLabel("E-mail");
        phoneLabel = new JLabel("Phone number");

        firstNameLabel.setBounds(10, 15, 150, 20);
        createPanel.add(firstNameLabel);
        firstNameField.setBounds(10, 35, 150, 20);
        createPanel.add(firstNameField);

        lastNameLabel.setBounds(10, 60, 150, 20);
        createPanel.add(lastNameLabel);
        lastNameField.setBounds(10, 80, 150, 20);
        createPanel.add(lastNameField);

        addressLabel.setBounds(10, 105, 150, 20);
        createPanel.add(addressLabel);
        createPanel.add(addressField);
        addressField.setBounds(10, 125, 150, 20);

        emailLabel.setBounds(10, 150, 150, 20);
        createPanel.add(emailLabel);
        createPanel.add(emailField);
        emailField.setBounds(10, 170, 150, 20);

        phoneLabel.setBounds(10, 195, 150, 20);
        createPanel.add(phoneLabel);
        createPanel.add(phoneField);
        phoneField.setBounds(10, 215, 150, 20);

        birthNumberLabel.setBounds(10, 240, 150, 20);
        createPanel.add(birthNumberLabel);
        birthNumberField.setBounds(10, 260, 150, 20);
        createPanel.add(birthNumberField);

        createReaderButton = new JButton();
        createReaderButton.setText("Add reader");
        createReaderButton.setBounds(10, 310, 150, 20);
        createPanel.add(createReaderButton);
        createReaderButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boolean check = true;
                if (firstNameField.getText().equals("")) {
                    firstNameField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (lastNameField.getText().equals("")) {
                    lastNameField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (birthNumberField.getText().equals("")) {
                    birthNumberField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (addressField.getText().equals("")) {
                    addressField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (emailField.getText().equals("")) {
                    emailField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (phoneField.getText().equals("")) {
                    phoneField.setBackground(Color.getHSBColor(243, 102, 78));
                    check = false;
                }
                if (check) {
                    ReaderTO reader = new ReaderTO();
                    reader.setFirstName(firstNameField.getText());
                    reader.setSurname(lastNameField.getText());
                    reader.setBirthNumber(birthNumberField.getText());
                    reader.setEmail(emailField.getText());
                    reader.setAddress(addressField.getText());
                    reader.setTelephoneNumber(phoneField.getText());
                    try {
                        reader = readerWebService.createReader(reader);
                        Object[] arr = {
                            reader.getId(), reader.getFirstName(), reader.getSurname(), reader.getBirthNumber(), reader.getEmail(), reader.getAddress(), reader.getTelephoneNumber()
                        };
                        model.addRow(arr);
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(readerPanel, "Cannot create reader.\nServer connection problem",
                                "Oops!", JOptionPane.ERROR_MESSAGE);
                    }

                    frameCreate.dispose();
                }
            }
        });
        return createPanel;
    }
}
